import { type CrudConfig, type FormFieldConfig } from '@/components';
import urlConstants from '@/constants/urlConstants';
import type { ProColumns } from '@ant-design/pro-components';

// 派案策略数据类型
export interface StrategySettingRecord {
  paramIndex?: number;
  strategyId: string;
  strategyName: string;
  assignmentMethod: string;
  strategyType: string;
  considerInventory: boolean;
  considerWeight: boolean;
  strategyDescription?: string;
  createTime?: string;
  updateTime?: string;
  updateUser?: string;
}

// 统一的页面配置函数 - 接收 translate 函数作为参数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const t = (key: string) => translate('strategyManage', key);
  const tCommon = (key: string) => translate('common', key);

  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.ADUIT_MANAGE.LIST,
        create: urlConstants.ADUIT_MANAGE.CREATE,
        update: urlConstants.ADUIT_MANAGE.EDIT,
        delete: urlConstants.ADUIT_MANAGE.DELETE,
        detail: urlConstants.ADUIT_MANAGE.LIST,
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'strategyName',
        label: t('strategyName'),
        type: 'input' as const,
      },
      {
        name: 'assignmentMethod',
        label: t('assignmentMethod'),
        type: 'select' as const,
        options: [
          { label: '自动派案', value: 'auto' },
          { label: '手动派案', value: 'manual' },
          { label: '混合派案', value: 'mixed' },
        ],
      },
      {
        name: 'strategyType',
        label: t('strategyType'),
        type: 'select' as const,
        options: [
          { label: '优先级策略', value: 'priority' },
          { label: '权重策略', value: 'weight' },
          { label: '轮询策略', value: 'round_robin' },
        ],
      },
    ],

    // 表格列配置
    columns: [
      {
        title: tCommon('paramIndex'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('strategyId'),
        dataIndex: 'strategyId',
        key: 'strategyId',
        width: 120,
      },
      {
        title: t('strategyName'),
        dataIndex: 'strategyName',
        key: 'strategyName',
        width: 150,
      },
      {
        title: t('assignmentMethod'),
        dataIndex: 'assignmentMethod',
        key: 'assignmentMethod',
        width: 120,
        valueEnum: {
          auto: { text: '自动派案', status: 'Default' },
          manual: { text: '手动派案', status: 'Warning' },
          mixed: { text: '混合派案', status: 'Success' },
        },
      },
      {
        title: t('strategyType'),
        dataIndex: 'strategyType',
        key: 'strategyType',
        width: 120,
        valueEnum: {
          priority: { text: '优先级策略', status: 'Default' },
          weight: { text: '权重策略', status: 'Warning' },
          round_robin: { text: '轮询策略', status: 'Success' },
        },
      },
      {
        title: t('considerInventory'),
        dataIndex: 'considerInventory',
        key: 'considerInventory',
        width: 100,
        render: (_, record) => (record.considerInventory ? tCommon('yes') : tCommon('no')),
      },
      {
        title: t('considerWeight'),
        dataIndex: 'considerWeight',
        key: 'considerWeight',
        width: 100,
        render: (_, record) => (record.considerWeight ? tCommon('yes') : tCommon('no')),
      },
      {
        title: t('strategyDescription'),
        dataIndex: 'strategyDescription',
        key: 'strategyDescription',
        width: 200,
        ellipsis: true,
      },
      {
        title: tCommon('updateUser'),
        dataIndex: 'updateUser',
        key: 'updateUser',
        width: 150,
      },
      {
        title: tCommon('updateTime'),
        dataIndex: 'updateTime',
        key: 'updateTime',
        width: 160,
        valueType: 'dateTime',
      },
    ] as ProColumns<StrategySettingRecord>[],

    // 表单字段配置
    formFields: [
      {
        name: 'strategyId',
        label: t('strategyId'),
        type: 'input' as const,
        rules: [{ required: true, message: t('strategyIdRequired') }],
      },
      {
        name: 'strategyName',
        label: t('strategyName'),
        type: 'input' as const,
        rules: [{ required: true, message: t('strategyNameRequired') }],
      },
      {
        name: 'assignmentMethod',
        label: t('assignmentMethod'),
        type: 'select' as const,
        options: [
          { label: '自动派案', value: 'auto' },
          { label: '手动派案', value: 'manual' },
          { label: '混合派案', value: 'mixed' },
        ],
        rules: [{ required: true, message: t('assignmentMethodRequired') }],
      },
      {
        name: 'strategyType',
        label: t('strategyType'),
        type: 'select' as const,
        options: [
          { label: '优先级策略', value: 'priority' },
          { label: '权重策略', value: 'weight' },
          { label: '轮询策略', value: 'round_robin' },
        ],
        rules: [{ required: true, message: t('strategyTypeRequired') }],
      },
      {
        name: 'considerInventory',
        label: t('considerInventory'),
        type: 'switch' as const,
      },
      {
        name: 'considerWeight',
        label: t('considerWeight'),
        type: 'switch' as const,
      },
      {
        name: 'strategyDescription',
        label: t('strategyDescription'),
        type: 'textarea' as const,
        span: 24,
        rules: [{ required: true, message: t('strategyDescriptionRequired') }],
      },
    ] as FormFieldConfig[],

    // 字典枚举配置
    dictEnum: {},
  };
};
